package com.example.permission.controller;

import com.example.permission.entity.Role;
import com.example.permission.entity.User;
import com.example.permission.service.RoleService;
import com.example.permission.service.UserService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;

    /**
     * 显示所有用户
     * @param map
     * @return
     */
    @GetMapping("/users")
    public String toUser(ModelMap map){
        List<User> all = userService.findAll();
        map.put("users",all);
        return "/user/list";
    }

    /**
     * 跳转到添加页面
     * @return
     */
    @GetMapping("/user")
    public String add(){
        return "/user/add";
    }

    /**
     * 添加方法
     * @param map
     * @param user
     * @return
     */
    @PostMapping("/users")
    public String toAdd(ModelMap map,User user){
        user.setStatus(0);
        //密码加密
        String password = user.getPassword();
        String encode = new BCryptPasswordEncoder().encode(password); //加密后的密码
        user.setPassword(encode);
        Integer add = userService.add(user);
        map.put("add",add);
        List<User> all = userService.findAll();
        map.put("users",all);
        return "/user/list";
    }

    /**
     * 跳转到修改页面
     * @param id
     * @param map
     * @return
     */
    @GetMapping("/user/{id}")
    public String update(@PathVariable Integer id,ModelMap map){
        User idAll = userService.getIdAll(id);
        map.put("user",idAll);
        return "/user/add";
    }

    /**
     * 修改方法
     * @param user
     * @param map
     * @return
     */
    @PutMapping("/users")
    public String toUpdate(User user,ModelMap map){
        userService.update(user);
        List<User> all = userService.findAll();
        map.put("users",all);
        return "/user/list";
    }

    /**
     * 删除方法
     * @param id
     * @param map
     * @return
     */
    @DeleteMapping("/user/{id}")
    public String delete(@PathVariable Integer id,ModelMap map){
        userService.delete(id);
        List<User> all = userService.findAll();
        map.put("users",all);
        return "/user/list";
    }

    /**
     * 更新用户角色
     * @param id
     * @param map
     * @return
     */
    @PatchMapping("/user/{id}")
    public String toPatch(@PathVariable Integer id,ModelMap map){
        List<Role> all = roleService.findAll();
        map.put("roles",all);
        List<Integer> roleIds = userService.getRoleIdsByUserId(id);
        map.put("roleIds",roleIds);
        return "/user/role";
    }
    @GetMapping("/userss/{id}")
    public String del(@PathVariable Integer id,ModelMap map){
        userService.deleteAll();
        List<User> all = userService.findAll();
        map.put("users",all);
        return "/user/list";
    }
    @GetMapping("/userss/{id}/{roleIds}")
    public String pathRole(@PathVariable Integer id,@PathVariable String roleIds,ModelMap map){
        System.out.println(id+roleIds);
        String[] arr = roleIds.split(",");//分割传进来的id
        List<Integer> ids = userService.getRoleIdsByUserId(id);//获取当前角色的id
        for(String s : arr){
            boolean flag = false;
            for(Integer integer:ids){ //循环之前已有的角色
                if(s.equals(integer+"")){ //然后作比较，如果刚穿进来的和已有的相同那就删除已有的，把新传进来的，添加进去
                    ids.remove(integer);
                    flag = true;
                    break;
                }
            }
            //如果flag是false那就证明没走上面的判断，走了判断进不去这个if，没走的话可以进去
            if(!flag){
                //方法进去后，执行添加方法
                userService.addRoleIdByUserId(id,Integer.valueOf(s));
            }
        }
        //执行删除方法
        ids.forEach(roleId -> userService.removeRoleIdByUserId(id,roleId));
        List<User> all = userService.findAll();
        map.put("users",all);
        return "/user/list";
    }

    @GetMapping("/users/{ids}")
    public String deletes(@PathVariable String ids,ModelMap map){
        String[] split = ids.split(",");
        for (String x: split) {
            userService.delete(Integer.parseInt(x));
        }
        List<User> all = userService.findAll();
        map.put("users",all);
        return "/user/list";
    }
}
